using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;

namespace Deepcode.SchedulerService
{
	/// <summary>
	/// Scheduler service invokes tasks from the task list file.
	/// The task list file is NGTasks.xml in the same directory as 
	/// the service executable.
	/// </summary>
	public partial class Scheduler : ServiceBase
	{
		// The timer used to invoke the schedule
		System.Timers.Timer t = null;

		// The scheduler configuration holding tasks
		SchedulerConfig c = null;
				
		/// <summary>
		/// Constructor - set service name and set the timer's 
		/// elapsed handler method.
		/// </summary>
		public Scheduler()
		{
			InitializeComponent();
			t = new System.Timers.Timer();
			c = new SchedulerConfig( this.EventLog );
		
			this.ServiceName = "Flux.NET Task Scheduler";
			t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
		}

		/// <summary>
		/// Occurs when the timer elapses
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
		{
			c.ProcessTasks();
		}

		/// <summary>
		/// Start the service - starts the timer
		/// </summary>
		/// <param name="args"></param>
		protected override void OnStart(string[] args)
		{
			t.Interval = c.ExecutionMinutes * 60 * 1000;
			t.Start();
		}

		/// <summary>
		/// Stop the service - stop the timer
		/// </summary>
		protected override void OnStop()
		{
			t.Stop();
		}
	}
}
